package com.jjwxc.jwjskandriod.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.activity.VipInfoActivity;
import com.jjwxc.jwjskandriod.adapter.VipBgViewHolder;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.base.FFApplication;
import com.jjwxc.jwjskandriod.framework.http.FFNetWork;
import com.jjwxc.jwjskandriod.framework.http.FFNetWorkCallBack;
import com.jjwxc.jwjskandriod.framework.imageloader.FFImageLoader;
import com.jjwxc.jwjskandriod.framework.refresh.recyclerview.BaseRecyclerAdapter;
import com.jjwxc.jwjskandriod.model.VIPBgResponse;
import com.jjwxc.jwjskandriod.model.VipDistanceResponse;
import com.jjwxc.jwjskandriod.model.request.UserProfileRequest;
import com.jjwxc.jwjskandriod.my.data.UserInfoBg;
import com.jjwxc.jwjskandriod.widget.LinearLayouDecoration;

import java.util.ArrayList;
import java.util.List;

public class VipBgFragment extends Fragment {
    private VipInfoActivity mActivity;
    private String value;
    private String key;
    private View view;
    private TextView tv_use;
    private RecyclerView rv_space;
    private VipBgAdapter adapter;

    private boolean userIsVip;
    private List<VIPBgResponse> mList = new ArrayList<>();

    public VipBgFragment(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            mActivity = (VipInfoActivity) getActivity();
            view = inflater.inflate(R.layout.fragment_viphead, container, false);
            setViewId();
            setListener();
        }
        return view;
    }

    public void setViewId() {
        tv_use = view.findViewById(R.id.tv_use);
        rv_space = view.findViewById(R.id.rv_rechargeinfo);
        rv_space.setNestedScrollingEnabled(false);
        rv_space.setFocusable(false);
        rv_space.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_space.addItemDecoration(new LinearLayouDecoration(getActivity(),
                LinearLayouDecoration.HORIZONTAL, R.dimen.border, R.color.transparent));
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public void setListener() {
        rv_space.getItemAnimator().setChangeDuration(0);// 通过设置动画执行时间为0来解决闪烁问题
        adapter = new VipBgAdapter(getActivity());
        rv_space.setAdapter(adapter);
        afterCreate();
        getVipDistance();
        tv_use.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onClick(View v) {
                if (!userIsVip) {
                    FFApplication.showToast("当前还不是VIP，暂时不能使用VIP功能");
                    return;
                }
                for (int i = 0; i < mList.size(); i++) {
                    if (mList.get(i).isSelect()) {
                        UserProfileRequest request = new UserProfileRequest();
                        request.setBackground(mList.get(i).getId());

                        mActivity.updateUserProfile(request);
                    }
                }
                FFApplication.showToast("背景已修改");
                adapter.notifyDataSetChanged();
            }
        });
    }


    public void afterCreate() {
        for (int i = 0; i < UserInfoBg.INSTANCE.getBACKGROUND().length; i++) {
            VIPBgResponse response = new VIPBgResponse();
            response.setId(UserInfoBg.INSTANCE.getBACKGROUND()[i]);
            response.setUrl(UserInfoBg.INSTANCE.getBACKGROUND_RID()[i]);

            if (UserInfoBg.INSTANCE.getBACKGROUND()[i].equals(mActivity.mBackground)) {
                response.setSelect(true);
            } else {
                response.setSelect(false);
            }

            mList.add(response);
        }

        adapter.setData(mList);
    }

    public void getVipDistance() {
        new FFNetWork().post(Url.vipDistance, null, new FFNetWorkCallBack<VipDistanceResponse>() {
            @Override
            public void onSuccess(VipDistanceResponse response) {
                if (response.getCode() == 200) {

                    userIsVip = response.getData() <= 0;

                }
            }

            @Override
            public boolean onFail(VipDistanceResponse response) {
                return false;
            }
        }, VipDistanceResponse.class);
    }
    public class VipBgAdapter extends BaseRecyclerAdapter<VipBgViewHolder> {
        private Context context;


        private List<VIPBgResponse> mData = new ArrayList<>();


        public VipBgAdapter(Context cnt) {
            this.context = cnt;

        }

        public void addData(List<VIPBgResponse> data) {
            if (data != null) {
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }

        public void setData(List<VIPBgResponse> data) {
            this.mData.clear();
            addData(data);
        }

        /**
         * 创建ViewHolder
         */
        @Override
        public VipBgViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {

            View mView = LayoutInflater.from(context).inflate(R.layout.mainpg_item, parent, false);
            VipBgViewHolder viewHolder_text = new VipBgViewHolder(mView);
            viewHolder_text.setIsRecyclable(false);
            return viewHolder_text;

        }

        @Override
        public VipBgViewHolder getViewHolder(View view) {
            return new VipBgViewHolder(view);
        }


        /**
         * 绑定数据：可以直接拿到已经绑定控件的Viewholder对象
         */
        @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
        @Override
        public void onBindViewHolder(final VipBgViewHolder holder, @SuppressLint("RecyclerView") final int position, boolean isItem) {
            FFImageLoader.loadImage(context, holder.img_grid, mData.get(position).getUrl(), true);
            if (mData.get(position).isSelect()) {
                holder.img_selct_one.setVisibility(View.VISIBLE);
                holder.cl_grid_item.setBackgroundResource(R.drawable.vip_bg_select_big);
            } else {
                holder.cl_grid_item.setBackgroundResource(R.color.transparent);
                holder.img_selct_one.setVisibility(View.INVISIBLE);
            }

            holder.img_selct_two.setVisibility(View.VISIBLE);
            holder.cl_grid_item.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("NotifyDataSetChanged")
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < mData.size(); i++) {
                        if (i == position) {
                            mData.get(i).setSelect(true);
                        } else {
                            mData.get(i).setSelect(false);
                        }
                    }
                    notifyDataSetChanged();
                }
            });


        }

        @Override
        public int getAdapterItemViewType(int position) {
            return position;
        }


        @Override
        public int getAdapterItemCount() {
            return mData.size();
        }


    }

}